#include "Cubo.h"


Cubo::Cubo(){
	//1 Cada cara del cubo con sus normales y coordenadas de textura
	this->m_vertices[0] = glm::vec3(1.0f, -1.0f, 1.0f);
	this->m_coords[0] = glm::vec2(0.0f, 1.0f);
	this->m_normales[0] = glm::vec3(1.0f, 0.0f, 0.0f);

	this->m_vertices[1] = glm::vec3(1.0f, -1.0f, -1.0f);
	this->m_coords[1] = glm::vec2(0.0f, 0.0f);
	this->m_normales[1] = glm::vec3(1.0f, 0.0f, 0.0f);

	this->m_vertices[2] = glm::vec3(1.0f, 1.0f, -1.0f);
	this->m_coords[2] = glm::vec2(1.0f, 0.0f);
	this->m_normales[2] = glm::vec3(1.0f, 0.0f, 0.0f);

	this->m_vertices[3] = glm::vec3(1.0f, 1.0f, 1.0f);
	this->m_coords[3] = glm::vec2(1.0f, 1.0f);
	this->m_normales[3] = glm::vec3(1.0f, 0.0f, 0.0f);

	//2
	this->m_vertices[4] = glm::vec3(1.0f, 1.0f, 1.0f);
	this->m_coords[4] = glm::vec2(0.0f, 1.0f);
	this->m_normales[4] = glm::vec3(0.0f, 1.0f, 0.0f);

	this->m_vertices[5] = glm::vec3(1.0f, 1.0f, -1.0f);
	this->m_coords[5] = glm::vec2(0.0f, 0.0f);
	this->m_normales[5] = glm::vec3(0.0f, 1.0f, 0.0f);

	this->m_vertices[6] = glm::vec3(-1.0f, 1.0f, -1.0f);
	this->m_coords[6] = glm::vec2(1.0f, 0.0f);
	this->m_normales[6] = glm::vec3(0.0f, 1.0f, 0.0f);
	
	this->m_vertices[7] = glm::vec3(-1.0f, 1.0f, 1.0f);
	this->m_coords[7] = glm::vec2(1.0f, 1.0f);
	this->m_normales[7] = glm::vec3(0.0f, 1.0f, 0.0f);

	//3
	this->m_vertices[8] = glm::vec3(-1.0f, 1.0f, 1.0f);
	this->m_coords[8] = glm::vec2(0.0f, 1.0f);
	this->m_normales[8] = glm::vec3(-1.0f, 0.0f, 0.0f);
	
	this->m_vertices[9] = glm::vec3(-1.0f, 1.0f, -1.0f);
	this->m_coords[9] = glm::vec2(0.0f, 0.0f);
	this->m_normales[9] = glm::vec3(-1.0f, 0.0f, 0.0f);
	
	this->m_vertices[10] = glm::vec3(-1.0f, -1.0f, -1.0f);
	this->m_coords[10] = glm::vec2(1.0f, 0.0f);
	this->m_normales[10] = glm::vec3(-1.0f, 0.0f, 0.0f);
	
	this->m_vertices[11] = glm::vec3(-1.0f, -1.0f, 1.0f);
	this->m_coords[11] = glm::vec2(1.0f, 1.0f);
	this->m_normales[11] = glm::vec3(-1.0f, 0.0f, 0.0f);

	//4
	this->m_vertices[12] = glm::vec3(-1.0f, -1.0f, 1.0f);
	this->m_coords[12] = glm::vec2(0.0f, 1.0f);
	this->m_normales[12] = glm::vec3(0.0f, -1.0f, 0.0f);
	
	this->m_vertices[13] = glm::vec3(-1.0f, -1.0f, -1.0f);
	this->m_coords[13] = glm::vec2(0.0f, 0.0f);
	this->m_normales[13] = glm::vec3(0.0f, -1.0f, 0.0f);
	
	this->m_vertices[14] = glm::vec3(1.0f, -1.0f, -1.0f);
	this->m_coords[14] = glm::vec2(1.0f, 0.0f);
	this->m_normales[14] = glm::vec3(0.0f, -1.0f, 0.0f);
	
	this->m_vertices[15] = glm::vec3(1.0f, -1.0f, 1.0f);
	this->m_coords[15] = glm::vec2(1.0f, 1.0f);
	this->m_normales[15] = glm::vec3(0.0f, -1.0f, 0.0f);

	//5
	this->m_vertices[16] = glm::vec3(-1.0f, 1.0f, 1.0f);
	this->m_coords[16] = glm::vec2(1.0f, 1.0f);
	this->m_normales[16] = glm::vec3(0.0f, 0.0f, 1.0f);
	
	this->m_vertices[17] = glm::vec3(-1.0f, -1.0f, 1.0f);
	this->m_coords[17] = glm::vec2(0.0f, 1.0f);
	this->m_normales[17] = glm::vec3(0.0f, 0.0f, 1.0f);
	
	this->m_vertices[18] = glm::vec3(1.0f, -1.0f, 1.0f);
	this->m_coords[18] = glm::vec2(0.0f, 0.0f);
	this->m_normales[18] = glm::vec3(0.0f, 0.0f, 1.0f);
	
	this->m_vertices[19] = glm::vec3(1.0f, 1.0f, 1.0f);
	this->m_coords[19] = glm::vec2(1.0f, 0.0f);
	this->m_normales[19] = glm::vec3(0.0f, 0.0f, 1.0f);

	//6
	this->m_vertices[20] = glm::vec3(1.0f, 1.0f, -1.0f);
	this->m_coords[20] = glm::vec2(1.0f, 1.0f);
	this->m_normales[20] = glm::vec3(0.0f, 0.0f, -1.0f);
	
	this->m_vertices[21] = glm::vec3(1.0f, -1.0f, -1.0f);
	this->m_coords[21] = glm::vec2(0.0f, 1.0f);
	this->m_normales[21] = glm::vec3(0.0f, 0.0f, -1.0f);
	
	this->m_vertices[22] = glm::vec3(-1.0f, -1.0f, -1.0f);
	this->m_coords[22] = glm::vec2(0.0f, 0.0f);
	this->m_normales[22] = glm::vec3(0.0f, 0.0f, -1.0f);
	
	this->m_vertices[23] = glm::vec3(-1.0f, 1.0f, -1.0f);
	this->m_coords[23] = glm::vec2(1.0f, 0.0f);
	this->m_normales[23] = glm::vec3(0.0f, 0.0f, -1.0f);

	// El indexbuffer de cada cara
	this->m_indexBuffer[0] = 0;
	this->m_indexBuffer[1] = 1;
	this->m_indexBuffer[2] = 2;
	this->m_indexBuffer[3] = 2;
	this->m_indexBuffer[4] = 3;
	this->m_indexBuffer[5] = 0;

	this->m_indexBuffer[6] = 4;
	this->m_indexBuffer[7] = 5;
	this->m_indexBuffer[8] = 6;
	this->m_indexBuffer[9] = 6;
	this->m_indexBuffer[10] = 7;
	this->m_indexBuffer[11] = 4;

	this->m_indexBuffer[12] = 8;
	this->m_indexBuffer[13] = 9;
	this->m_indexBuffer[14] = 10;
	this->m_indexBuffer[15] = 10;
	this->m_indexBuffer[16] = 11;
	this->m_indexBuffer[17] = 8;

	this->m_indexBuffer[18] = 12;
	this->m_indexBuffer[19] = 13;
	this->m_indexBuffer[20] = 14;
	this->m_indexBuffer[21] = 14;
	this->m_indexBuffer[22] = 15;
	this->m_indexBuffer[23] = 12;

	this->m_indexBuffer[24] = 16;
	this->m_indexBuffer[25] = 17;
	this->m_indexBuffer[26] = 18;
	this->m_indexBuffer[27] = 18;
	this->m_indexBuffer[28] = 19;
	this->m_indexBuffer[29] = 16;

	this->m_indexBuffer[30] = 20;
	this->m_indexBuffer[31] = 21;
	this->m_indexBuffer[32] = 22;
	this->m_indexBuffer[33] = 22;
	this->m_indexBuffer[34] = 23;
	this->m_indexBuffer[35] = 20;
}


void Cubo::renderizar(const glm::mat4 &matVista, const glm::mat4 &matProyeccion, const glm::mat4 &matModelo) const {
	// Carga el shader
	Renderizable::cargarShaderProgram();

	// Empieza a dibujar todo
	glEnableClientState(GL_VERTEX_ARRAY);
	glEnableClientState(GL_NORMAL_ARRAY);
	glEnableClientState(GL_TEXTURE_COORD_ARRAY);

	// Los datos de iluminacion, transformacion y textura
	Renderizable::cargarDatosIluminacion(matModelo, matVista);
	Renderizable::cargarMatricesTransformacion(matVista, matProyeccion, matModelo);
	Renderizable::cargarTextura();

	// El cubo
	glVertexPointer(3, GL_FLOAT, 0, this->m_vertices);
	glNormalPointer(GL_FLOAT, 0, this->m_normales);
	glTexCoordPointer(2, GL_FLOAT, 0, this->m_coords);
	glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_INT, this->m_indexBuffer);

	// Listo
	glDisableClientState(GL_TEXTURE_COORD_ARRAY);
	glDisableClientState(GL_NORMAL_ARRAY);
	glDisableClientState(GL_VERTEX_ARRAY);
}

